package com.dianping.cat.consumer.dependency.model.transform;
import com.dianping.cat.consumer.dependency.model.IVisitor;
import com.dianping.cat.consumer.dependency.model.entity.Dependency;
import com.dianping.cat.consumer.dependency.model.entity.DependencyReport;
import com.dianping.cat.consumer.dependency.model.entity.Index;
import com.dianping.cat.consumer.dependency.model.entity.Segment;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

public class DefaultNativeParser implements IVisitor {
  private DefaultLinker m_linker;

  public DefaultNativeParser(InputStream in) {
/*  16 */     this.m_linker = new DefaultLinker(true);




/*  21 */     this.m_in = new DataInputStream(in);
  }
  private DataInputStream m_in;

  public static DependencyReport parse(byte[] data) { return parse(new ByteArrayInputStream(data)); }


  public static DependencyReport parse(InputStream in) {
    DefaultNativeParser parser = new DefaultNativeParser(in);
/*  30 */     DependencyReport dependencyReport = new DependencyReport();

    try {
/*  33 */       dependencyReport.accept(parser);
/*  34 */     } catch (RuntimeException e) {
/*  35 */       if (e.getCause() == null || !(e.getCause() instanceof java.io.EOFException))
      {

/*  38 */         throw e;
      }
    }

/*  42 */     parser.m_linker.finish();
/*  43 */     return dependencyReport;
  }



  public void visitDependency(Dependency dependency) {
    byte tag;
/*  50 */     while ((tag = readTag()) != -1) {
/*  51 */       visitDependencyChildren(dependency, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitDependencyChildren(Dependency dependency, int _field, int _type) {
/*  56 */     switch (_field) {
      case 1:
/*  58 */         dependency.setType(readString());
        break;
      case 2:
/*  61 */         dependency.setTarget(readString());
        break;
      case 3:
/*  64 */         dependency.setTotalCount(readLong());
        break;
      case 4:
/*  67 */         dependency.setAvg(readDouble());
        break;
      case 5:
/*  70 */         dependency.setErrorCount(readLong());
        break;
      case 6:
/*  73 */         dependency.setKey(readString());
        break;
      case 7:
/*  76 */         dependency.setSum(readDouble());
        break;
    }
  }



  public void visitDependencyReport(DependencyReport dependencyReport) {
    byte tag;
/*  85 */     if ((tag = readTag()) != -4) {
/*  86 */       throw new RuntimeException(String.format("Malformed payload, expected: %s, but was: %s!", new Object[] { Integer.valueOf(-4), Byte.valueOf(tag) }));
    }

/*  89 */     while ((tag = readTag()) != -1) {
/*  90 */       visitDependencyReportChildren(dependencyReport, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitDependencyReportChildren(DependencyReport dependencyReport, int _field, int _type) {
/*  95 */     switch (_field) {
      case 1:
/*  97 */         dependencyReport.setDomain(readString());
        break;
      case 2:
/* 100 */         dependencyReport.setStartTime(readDate());
        break;
      case 3:
/* 103 */         dependencyReport.setEndTime(readDate());
        break;
      case 4:
/* 106 */         if (_type == 1) {
/* 107 */           dependencyReport.addDomainName(readString()); break;
/* 108 */         }  if (_type == 2) {
/* 109 */           for (int i = readInt(); i > 0; i--) {
/* 110 */             dependencyReport.addDomainName(readString());
          }
        }
        break;
      case 33:
/* 115 */         if (_type == 1) {
/* 116 */           Segment segment = new Segment();

/* 118 */           visitSegment(segment);
/* 119 */           this.m_linker.onSegment(dependencyReport, segment); break;
/* 120 */         }  if (_type == 2) {
/* 121 */           for (int i = readInt(); i > 0; i--) {
/* 122 */             Segment segment = new Segment();

/* 124 */             visitSegment(segment);
/* 125 */             this.m_linker.onSegment(dependencyReport, segment);
          }
        }
        break;
    }
  }



  public void visitIndex(Index index) {
    byte tag;
/* 136 */     while ((tag = readTag()) != -1) {
/* 137 */       visitIndexChildren(index, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitIndexChildren(Index index, int _field, int _type) {
/* 142 */     switch (_field) {
      case 1:
/* 144 */         index.setName(readString());
        break;
      case 2:
/* 147 */         index.setTotalCount(readLong());
        break;
      case 3:
/* 150 */         index.setErrorCount(readLong());
        break;
      case 4:
/* 153 */         index.setSum(readDouble());
        break;
      case 5:
/* 156 */         index.setAvg(readDouble());
        break;
    }
  }



  public void visitSegment(Segment segment) {
    byte tag;
/* 165 */     while ((tag = readTag()) != -1) {
/* 166 */       visitSegmentChildren(segment, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitSegmentChildren(Segment segment, int _field, int _type) {
/* 171 */     switch (_field) {
      case 1:
/* 173 */         segment.setId(Integer.valueOf(readInt()));
        break;
      case 2:
/* 176 */         segment.setExceptionCount(Integer.valueOf(readInt()));
        break;
      case 33:
/* 179 */         if (_type == 1) {
/* 180 */           Index index = new Index();

/* 182 */           visitIndex(index);
/* 183 */           this.m_linker.onIndex(segment, index); break;
/* 184 */         }  if (_type == 2) {
/* 185 */           for (int i = readInt(); i > 0; i--) {
/* 186 */             Index index = new Index();

/* 188 */             visitIndex(index);
/* 189 */             this.m_linker.onIndex(segment, index);
          }
        }
        break;
      case 34:
/* 194 */         if (_type == 1) {
/* 195 */           Dependency dependency = new Dependency();

/* 197 */           visitDependency(dependency);
/* 198 */           this.m_linker.onDependency(segment, dependency); break;
/* 199 */         }  if (_type == 2) {
/* 200 */           for (int i = readInt(); i > 0; i--) {
/* 201 */             Dependency dependency = new Dependency();

/* 203 */             visitDependency(dependency);
/* 204 */             this.m_linker.onDependency(segment, dependency);
          }
        }
        break;
    }
  }

  private Date readDate() {
    try {
/* 213 */       return new Date(readVarint(64));
/* 214 */     } catch (IOException e) {
/* 215 */       throw new RuntimeException(e);
    }
  }

  private double readDouble() {
    try {
/* 221 */       return Double.longBitsToDouble(readVarint(64));
/* 222 */     } catch (IOException e) {
/* 223 */       throw new RuntimeException(e);
    }
  }

  private int readInt() {
    try {
/* 229 */       return (int)readVarint(32);
/* 230 */     } catch (IOException e) {
/* 231 */       throw new RuntimeException(e);
    }
  }

  private long readLong() {
    try {
/* 237 */       return readVarint(64);
/* 238 */     } catch (IOException e) {
/* 239 */       throw new RuntimeException(e);
    }
  }

  private String readString() {
    try {
/* 245 */       return this.m_in.readUTF();
/* 246 */     } catch (IOException e) {
/* 247 */       throw new RuntimeException(e);
    }
  }

  private byte readTag() {
    try {
/* 253 */       return this.m_in.readByte();
/* 254 */     } catch (IOException e) {
/* 255 */       throw new RuntimeException(e);
    }
  }

  protected long readVarint(int length) throws IOException {
/* 260 */     int shift = 0;
/* 261 */     long result = 0L;

/* 263 */     while (shift < length) {
/* 264 */       byte b = this.m_in.readByte();
/* 265 */       result |= (b & 0x7F) << shift;
/* 266 */       if ((b & 0x80) == 0) {
/* 267 */         return result;
      }
/* 269 */       shift += 7;
    }

/* 272 */     throw new RuntimeException("Malformed variable int " + length + "!");
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-consumer-3.0.0.jar!/com/dianping/cat/consumer/dependency/model/transform/DefaultNativeParser.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */